package eco2automotive.gui;
import eco2automotive.port.ConnectionControl;
import eco2automotive.elmFilter.Filter;
import eco2automotive.util.MathLib;
import eco2automotive.gui.components.Panel_MONITOR;
import eco2automotive.gui.components.Panel_PROGRESS;
import eco2automotive.data.HashParameters;
import eco2automotive.data.Parameter;
import java.util.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.*;
import java.awt.Dimension;

/**  
  * * Show a modal dialog: the monitoring of the parameters.
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
  *
  */ 
public class Layout_MONITORING extends JDialog{
        private eco2automotive parent;       
        private Panel_MONITOR PANEL_MONITOR;        
        private Panel_PROGRESS PANELPROGRESS;
        private ConnectionControl connControl;              
        private HashParameters h;
        private JTextField info_tx_data;

      /** 
     * Create a new Panel_DTCs and a Panel_PRROGRESS
     * @param parent is the root JFrame
     * @param h contains a HashParameters
     * @see eco2automotive.data.HashParameters
    */      
    public Layout_MONITORING(JFrame parent, HashParameters h){	
          super(parent);                  
          setTitle("Monitoring...");   
          addWindowListener( new WindowAdapter() {
 public void windowClosing( WindowEvent e ) {
 close();
 }
 });
       this.h=h;
       this.info_tx_data=new JTextField();
       this.info_tx_data.setEditable(false);       
      this.add(info_tx_data, BorderLayout.NORTH);
      this.add(PANEL_MONITOR=new Panel_MONITOR(h),BorderLayout.CENTER);
      this.add(PANELPROGRESS=new Panel_PROGRESS(h.getHash().size()),BorderLayout.SOUTH);
       this.parent=(eco2automotive)parent;              
        setSize(new Dimension(800, 500));        
        setResizable(true);
        //pack();               
    }    
   /** 
     * Update the panel of information: Panel_PROGRESS
     * @see eco2automotive.gui.components.Panel_PROGRESS 
     * @param info String to display
    */
    
public void updatePrgogress(String info){
    this.PANELPROGRESS.updateLabel("TRANSFERING DATA...... "+info);
    this.PANELPROGRESS.advance();
}
    
 private void close() {     
 dispose();
 }
 
 
   /** 
     * Filter, Calculate & Update the information in the panel PANEL_MONITOR
  *  @see eco2automotive.util.MathLib
    * @see eco2automotive.elmFilter.Filter
     * @see eco2automotive.gui.components.Panel_MONITOR   
     * @param data String to filter
    */
 
 public void updateData(String data){                     
     System.out.println("Panel_MONITOR...  Fiter the data: "+data);      
     this.info_tx_data.setText(data);    
    Iterator it = h.getHash().entrySet().iterator();
    while (it.hasNext()) { 
        Map.Entry e = (Map.Entry)it.next();
        //e.getKey();
        Parameter p = (Parameter)e.getValue();        
//Example: "str_find=410D[0-9-A-Z]{4}"        
      int calc4x=Integer.parseInt(p.getOperationMode())+40; //Obtains 4(x)        
      String str_find=String.valueOf(calc4x).concat(p.getPidKey()).concat("[0-9-A-Z]{"); //Obtains 4(x)(pid)[0-9-A-Z]{       
      str_find=str_find.concat(String.valueOf(p.getBytesReturned()*2).concat("}"));      //2 char = 1byte
          
      String str= new Filter().getDigest(data,str_find); 
      
      if (str.length()>0 && p.getBytesReturned()>0){                  
          String masc=p.getPID(0).getMasc();          
          //eg. mask > AABBCCDD          
          str=str.substring((p.getOperationMode().length()+p.getPidKey()).length()+1);                                                                     
          for(int i=0;i<=p.getNumberofPids()-1;i++) 
           {                                               
               String fx=p.getPID(i).getFormula().toString();                                       
               String calc=new MathLib().calculatorXF(str,p.getPID(i).getMasc(),p.getPID(i).getFormula());               
                 this.PANEL_MONITOR.updateData(calc+"<"+str+">",p.getOperationMode(),p.getPidKey(),i); //Update the JTable                                             
          }
          
      }                        
    }
    
}
    
    
}
